package com.myh.cc.coom.search.adapter

import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.ViewGroup
import com.myh.cc.coom.search.bean.HotBean
import com.myh.cc.coom.search.viewmodel.SearchModel
import com.myh.cc.databinding.FragmentSearchBinding
import com.myh.cc.databinding.ItemHotBinding

/**
 * des    ：
 * create    ：  mayahao.
 * date   ：  2018/1/15.   ：
 */
class HotAdapter(bean: HotBean,modle: SearchModel,binder: FragmentSearchBinding): RecyclerView.Adapter<HotAdapter.VH>() {

    val bean = bean
    private val model = modle
    private val binder = binder

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VH {

        return VH.create(LayoutInflater.from(parent.context),parent)
    }

    override fun onBindViewHolder(holder: VH, position: Int) {

        holder.binding.hotBt.text = bean.data.hotkey[position].k

        holder.binding.hotBt.setOnClickListener{

            binder.searchEt.setText(holder.binding.hotBt.text.toString())
            model.getSearch(holder.binding.hotBt.text.toString().replace(" ",""))

        }
    }

    override fun getItemCount(): Int {
        return    if(bean.data.hotkey.size >= 11) 11 else bean.data.hotkey.size
    }


    class VH(binding: ItemHotBinding): RecyclerView.ViewHolder(binding.root){

        val binding = binding

        companion object {

            fun create(inflater: LayoutInflater, parent: ViewGroup): HotAdapter.VH {
                val binder = ItemHotBinding.inflate(inflater, parent, false)
                return VH(binder)
            }
        }

    }

}